#!/sbin/openrc-run
# Distributed under the terms of the GNU General Public License v3
# Written by Bernhard Tittelbach based on examples from Gentoo, openvpn and anytun debian init.rd script

opts="${opts} reload"
depend() {
	need net
	use dns
	after bootmisc
}

DAEMON=/usr/sbin/anytun
ANYTUNCONFIG=/usr/bin/anytun-config
CONTROLDAEMON=/usr/bin/anytun-controld
NAME=anytun
DESC=anytun
CONFIG_DIR=/etc/anytun
VARCONFIG_DIR=/var/run/anytun-controld
VARRUN_DIR=/var/run/anytun

VPN=${SVCNAME#*.}
[ "$VPN" = "$SVCNAME" ] && VPN=""

# Include anytun defaults if available
if [ -f /etc/conf.d/anytun ] ; then
	. /etc/conf.d/anytun
fi

test -x $DAEMON || exit 1


start_vpn () {
	if [ -f $CONFIG_DIR/$NAME/config ] ; then
		POSTUP=''
		test -f  $CONFIG_DIR/$NAME/post-up.sh && POSTUP="-x $CONFIG_DIR/$NAME/post-up.sh"
		CHROOTDIR=`grep '^chroot' < $CONFIG_DIR/$NAME/config | sed 's/chroot\s*//'`
		if [ -n "$CHROOTDIR" ] ; then
			test -d $CHROOTDIR || mkdir -p $CHROOTDIR
		fi
		test -d $VARRUN_DIR || mkdir -p $VARRUN_DIR
		DAEMONARG=`sed 's/#.*//' < $CONFIG_DIR/$NAME/config | grep -e '\w' | sed  's/^/--/' | tr '\n' ' '`
		start-stop-daemon --start --pidfile $VARRUN_DIR/${NAME}.pid --exec $DAEMON -- --write-pid $VARRUN_DIR/${NAME}.pid $POSTUP \
			$DAEMONOPTS $DAEMONARG || return 1
	else
		eerror "no config found"
		return 1
	fi
	start_configd
	return 0
}
stop_vpn () {
	start-stop-daemon --stop --pidfile $PIDFILE --exec $DAEMON
	rm -f $PIDFILE
	stop_configd
}

start_configd () {
	if [ -d $CONFIG_DIR/$NAME/conf.d ] ; then
		test -d $VARCONFIG_DIR && chmod 700 $VARCONFIG_DIR || checkpath -d -m 0700 $VARCONFIG_DIR
		rm -f $VARCONFIG_DIR/$NAME 2>/dev/null
		KDPRF=`sed 's/#.*//'  <  $CONFIG_DIR/$NAME/config | grep -e 'kd-prf' | sed  's/^/ --/' | xargs echo`
		for CLIENTPATH in $CONFIG_DIR/$NAME/conf.d/* ; do
			DAEMONARG=`sed 's/#.*//'  <  $CLIENTPATH | grep -e '\w' | sed  's/^/ --/' | xargs echo`
			$ANYTUNCONFIG $DAEMONARG $CIPHER $AUTHALGO $KDPRF >> $VARCONFIG_DIR/$NAME
		done
		CONTROLHOST=`sed 's/#.*//'  <  $CONFIG_DIR/$NAME/config | grep -e 'control-host' | sed  's/^/ --/'i | xargs echo`
		start-stop-daemon --start --pidfile $VARCONFIG_DIR/${NAME}.pid --exec $CONTROLDAEMON \
			-- -f $VARCONFIG_DIR/$NAME $DAEMONOPTS $CONTROLHOST --write-pid $VARCONFIG_DIR/${NAME}.pid
	fi
}
stop_configd () {
	if [ -d $CONFIG_DIR/$NAME/conf.d ] ; then
		start-stop-daemon --stop --pidfile $VARCONFIG_DIR/${NAME}.pid --exec $CONTROLDAEMON
		rm -f $VARCONFIG_DIR/${NAME}.pid
	fi 
}

start () {
	if test -z "$VPN" ; then
		if [ -f $CONFIG_DIR/autostart ] ; then
			for NAME in `sed 's/#.*//'  <  $CONFIG_DIR/autostart | grep -e '\w'`; do
				ebegin "Starting ${DESC} VPN: ${NAME}"
				start_vpn
				eend $?
			done
		else
			eerror "no config found"
			return 1;
		fi
	else
			NAME="$VPN"
			ebegin "Starting ${DESC} VPN: ${NAME}"
			start_vpn
			eend $?
	fi
}

stop () {
	if test -z "$VPN" ; then
		for PIDFILE in ${VARRUN_DIR}/*.pid ; do
			NAME=`basename $PIDFILE .pid`
			ebegin "Stopping ${DESC} VPN: ${NAME}"
			stop_vpn
			eend $?
		done
	else
		if test -e ${VARRUN_DIR}/${VPN}.pid ; then
			PIDFILE=${VARRUN_DIR}/${VPN}.pid
			NAME=`basename $PIDFILE .pid`
			ebegin "Stopping ${DESC} VPN: ${NAME}"
			stop_vpn
			eend $?
		else
			eerror " failure: No such tunnel is running: $VPN"
		fi
	fi
}

reload () {
	if test -z "$VPN" ; then
		for PIDFILE in ${VARRUN_DIR}/*.pid ; do
			NAME=`basename $PIDFILE .pid`
			if [ -d $CONFIG_DIR/$NAME/conf.d ] ; then
				ebegin "Reloading ${DESC} VPN: ${NAME}"
				stop_vpn
				start_vpn
				eend $?
			else
				ebegin "Reloading ${DESC} VPN: ${NAME}"
				stop_configd
				start_configd
				eend $?
			fi
		done
	else
		if test -e ${VARRUN_DIR}/${VPN}.pid ; then
			PIDFILE=${VARRUN_DIR}/${VPN}.pid
			NAME=`basename $PIDFILE .pid`
			if [ -d $CONFIG_DIR/$NAME/conf.d ] ; then
				ebegin "Reloading ${DESC} VPN: ${NAME}"
				stop_vpn
				start_vpn
				eend $?
			else
				ebegin "Reloading ${DESC} VPN: ${NAME}"
				stop_configd
				start_configd
				eend $?
			fi
		else
			eerror "failure: No such tunnel is running: $VPN"
		fi
	fi
}
